package com.ruoyi.imports.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.imports.domain.ProcessExcelData;
import com.ruoyi.imports.domain.RawWaterExcelData;
import com.ruoyi.imports.listener.ProcessExcelDataListener;
import com.ruoyi.imports.listener.RawWaterExcelListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.imports.domain.TdProcessDataType;
import com.ruoyi.imports.service.ITdProcessDataTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 过程数据检测类型Controller
 *
 * @author ruoyi
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/type")
public class TdProcessDataTypeController extends BaseController {
    @Autowired
    private ITdProcessDataTypeService tdProcessDataTypeService;

    /**
     * 查询过程数据检测类型列表
     */
    @RequiresPermissions("imports:type:list")
    @GetMapping("/list")
    public TableDataInfo list(TdProcessDataType tdProcessDataType) {
        startPage();
        List<TdProcessDataType> list = tdProcessDataTypeService.selectTdProcessDataTypeList(tdProcessDataType);
        return getDataTable(list);
    }

    /**
     * 导出过程数据检测类型列表
     */
    @RequiresPermissions("imports:type:export")
    @Log(title = "过程数据检测类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdProcessDataType tdProcessDataType) {
        List<TdProcessDataType> list = tdProcessDataTypeService.selectTdProcessDataTypeList(tdProcessDataType);
        ExcelUtil<TdProcessDataType> util = new ExcelUtil<TdProcessDataType>(TdProcessDataType.class);
        util.exportExcel(response, list, "过程数据检测类型数据");
    }

    /**
     * 获取过程数据检测类型详细信息
     */
    @RequiresPermissions("imports:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tdProcessDataTypeService.selectTdProcessDataTypeById(id));
    }

    /**
     * 新增过程数据检测类型
     */
    @RequiresPermissions("imports:type:add")
    @Log(title = "过程数据检测类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdProcessDataType tdProcessDataType) {
        return toAjax(tdProcessDataTypeService.insertTdProcessDataType(tdProcessDataType));
    }

    /**
     * 修改过程数据检测类型
     */
    @RequiresPermissions("imports:type:edit")
    @Log(title = "过程数据检测类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdProcessDataType tdProcessDataType) {
        return toAjax(tdProcessDataTypeService.updateTdProcessDataType(tdProcessDataType));
    }

    /**
     * 删除过程数据检测类型
     */
    @RequiresPermissions("imports:type:remove")
    @Log(title = "过程数据检测类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tdProcessDataTypeService.deleteTdProcessDataTypeByIds(ids));
    }


    //    @RequiresPermissions("imports:imports:import")
    @Log(title = "过程数据监测表", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file,
                                  @RequestParam(value = "projectId", required = false) Long projectId) {
        try {
            // 1. 创建监听器
            ProcessExcelDataListener listener = new ProcessExcelDataListener();

            // 2. 使用监听器读取 Excel 数据
            EasyExcel.read(file.getInputStream(), ProcessExcelData.class, listener)
                    .headRowNumber(4)
                    .sheet(1) // 读取第一个 sheet
                    .doRead(); // 执行读取

            // 3. 从监听器获取解析后的数据
            List<ProcessExcelData> excelDataList = listener.getDataList();

            // 4. 检查数据是否为空
            if (excelDataList == null || excelDataList.isEmpty()) {
                return error("Excel 文件中未找到有效数据");
            }

            // 5. 调用 Service 处理导入逻辑
            tdProcessDataTypeService.importProcessDataType(excelDataList, projectId);

            return success("导入成功，共导入 " + excelDataList.size() + " 条数据");

        } catch (IOException e) {
            return error("导入失败：文件读取异常，" + e.getMessage());
        } catch (Exception e) {
            return error("导入失败：" + e.getMessage());
        }
    }
}
